#pragma once

#include "net/defines.h"
#include "service/boradcast.h"
#include "libzqutil/log.h"
#include "json11/json11.hpp"
#include "service/protocol.h"
#include "service/client.h"
#include "service/server.h"
#include "service/finder.h"
#include "service/register.h"
#include "service/request.h"
#include "service/balance.h"
#include "service/method_mgr.h"
#include "libzqutil/ini/SimpleIni.h"

using namespace libzq;
namespace jrpc
{
	class JsonProtocol :
		public ProtocolImpl
	{
	public:
		JsonProtocol() {}
		JsonProtocol(json11::Json::object& o);
		bool is_null() override;
		void parse(const string& in, string& err) override;
		string dumps() const override;

		bool is_error() override;
		std::tuple<int32_t, string> error() override;
		void set_error(const int errorno, const char* errmsg) override;

		void set_method(const string& method) override;
		string method() override;

		const json11::Json& args() const;
	private:
		json11::Json _obj;
	};

	json11::Json json_null(const string& data);
	json11::Json json_load(const string& data);

	using JsonHandler = std::function<JsonProtocol(const JsonProtocol& req)>;
	using JsonHttpFinder = HttpServerFinder<JsonProtocol, RobinBalancer<JsonProtocol>>;
	using JsonRequest = Request<JsonProtocol, JsonHttpFinder>;
	using JsonClient = Client<JsonProtocol>;
	using JsonMethodMgr = DllMethodMgr<JsonHandler>;
	using JsonServer = Server<JsonProtocol, JsonHandler, JsonMethodMgr, HttpRegister>;
	using JsonBoradcast = Boradcast<JsonProtocol>;
}